package com.iot.gateway.aspectj.converter;

import com.iot.gateway.annotation.FieldConverter;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * 时间戳转换器
 */
public class TimestampConverter implements FieldConverter {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
    @Override
    public Object convert(Object value) {
        if (value instanceof Long) {
            // 将时间戳转换为本地时区的日期时间字符串
            return FORMATTER.format(
                    LocalDateTime.ofInstant(Instant.ofEpochMilli((Long) value), ZoneId.systemDefault())
            );
        }
        return value;
    }

}
